package flex.cc.job.executor.jobhandler;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import flex.cc.PublicUtil;
import flex.cc.job.executor.util.KettleUtil;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.service.DictCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;

@Component
@JobHandler(value="kettleHandler")
public class KettleHandler  extends IJobHandler{

	private static final String KETTLE_INFO = "KettleInfo";
	@Autowired
	private DictCodeService dictCodeService;
	
	@Override
	public ReturnT<String> execute(String param) throws Exception {
		
		XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行定时任务传入参数的信息 ("+param+")-->>>>>>>>>>>>>>>>");
		//获取系统当前时间
		String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		String comCode = "";  //总公司comcode
		Map<String,String> tsMap = new HashMap<String,String>();//存放脚本执行所需要的环境变量及值
		
		String[] params = param.split(",");
		int paramLen = params.length;
		
		
		String pathvalue = null;
		String comtype = null;

		// TODO 读取数据字典,需要自己熟悉下
		DictCodeEntity dictCodeEntity = dictCodeService.searchAllCodeByTypeCode(KETTLE_INFO, params[0], 1, comCode);
		String kettleinfo = dictCodeEntity.getValue();

		if(!PublicUtil.isEmpty(kettleinfo)) {
			String[] kettleinfoArr = kettleinfo.split(";");
			if(kettleinfoArr.length==1) {
				pathvalue = kettleinfoArr[0];
			}else if(kettleinfoArr.length==2){
				pathvalue = kettleinfoArr[0];
				comtype = kettleinfoArr[1];
				if(!PublicUtil.isEmpty(comtype)) {
					tsMap.put("comtype", comtype);
				}
			}
		}
		
		if(PublicUtil.isEmpty(pathvalue)) {
			
			String st = "";
			String et = "";
			if(paramLen == 1) {
				String time = KettleUtil.getTime30_15(nowTime);
				String[] ts = time.split(",");
				st = ts[0];
				et = ts[1];
			}else if(paramLen == 2) {
				if("beforeOneDay".equals(params[1])) {
					String[] ts = KettleUtil.getTime().split(",");
					st = ts[0];
					et = ts[1];
				}else if("beforeThreeDay".equals(params[1])) {//当前时间的前三天 
					String[] ts = KettleUtil.getThreeTime().split(",");
					st = ts[0];
					et = ts[1];
				}else {
					String[] ts = KettleUtil.getTime(params[1]).split(",");
					st = ts[0];
					et = ts[1];
				}
			}else if(paramLen == 3) {
				st = params[1];
				et = params[2];
			}else if(paramLen == 4) {
				//取传入的时间范围中的数据 按天取
				if("dateRange".equals(params[1])) {
					List<String> timeList = KettleUtil.getTimeList(params[2], params[3]);
					for (String string : timeList) {
						String[] ts = string.split(",");
						st = ts[0];
						et = ts[1];
					}
				}else {
					String[] ts = KettleUtil.getTime(params[1], Long.parseLong(params[2]), Long.parseLong(params[3])).split(",");
					st = ts[0];
					et = ts[1];
				}
			}else {
				XxlJobLogger.log("<<<<<<<<<<<<<<<<--未知时间-->>>>>>>>>>>>>>>>");
			}
			XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行路径未获取到---时间区间( "+st+" ~ "+ et +")->>>>>>>>>>>>>>>>");
			return FAIL;
		}
		String p = pathvalue.substring(0, pathvalue.lastIndexOf('/'));//脚本路径的父目录
		tsMap.put("Internal.Job.Filename.Directory", p);
		
		
		
		if(paramLen == 1) {//  参数为一个参数：key 根据key从数据字典里取脚本执行路径   执行时间是当前时间半小时前到该时间往后偏移15分钟  参数格式：kettle:path

			try {
				String time = KettleUtil.getTime30_15(nowTime);
				String[] ts = time.split(",");
				tsMap.put("startdate", ts[0]);
				tsMap.put("enddate", ts[1]);
				
				XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行路径："+ pathvalue +"-->>>>>>>>>>>>>>>>");
				XxlJobLogger.log("(定时任务执行时间为： " + nowTime );
				XxlJobLogger.log(" 与传输数据的时间校验的时间： " + nowTime );
				XxlJobLogger.log(" 传输数据的时间区间： "+ ts[0] + " ~ " + ts[1] + ")");
				
				KettleUtil.run(tsMap,pathvalue);
				
				XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行结束-->>>>>>>>>>>>>>>>");
				return SUCCESS;
			} catch (Exception e) {
				XxlJobLogger.log(">>>>>>>>>>>>>>>>执行失败<<<<<<<<<<<<<<<" + e.toString());
				return FAIL;
			}
			
		}else if(paramLen == 2){//当前时间前一天/指定时间前一天    参数格式：kettle:path,beforeOneDay或2094-04-03 19:15:00
			
			try {
				String startDate = "";
				String endDate = "";
				
				if("beforeOneDay".equals(params[1])) {
					String[] ts = KettleUtil.getTime().split(",");
					startDate = ts[0];
					endDate = ts[1];
				}else if("beforeThreeDay".equals(params[1])) {//当前时间的前三天 
					String[] ts = KettleUtil.getThreeTime().split(",");
					startDate = ts[0];
					endDate = ts[1];
				}else {
					String[] ts = KettleUtil.getTime(params[1]).split(",");
					startDate = ts[0];
					endDate = ts[1];
				}
				
				tsMap.put("startdate", startDate);
				tsMap.put("enddate", endDate);
				
				XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行路径："+ pathvalue +"-->>>>>>>>>>>>>>>>");
				XxlJobLogger.log("(定时任务执行时间为： " + nowTime );
				
				XxlJobLogger.log(" 与传输数据的时间校验的时间： " + ("beforeOneDay".equals(params[1])?nowTime:params[1]));
				XxlJobLogger.log(" 传输数据的时间区间： "+ startDate + " ~ " + endDate + ")");
				
				KettleUtil.run(tsMap,pathvalue);
				
				XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行结束-->>>>>>>>>>>>>>>>");
				return SUCCESS;
			} catch (Exception e) {
				XxlJobLogger.log(">>>>>>>>>>>>>>>>执行失败<<<<<<<<<<<<<<<" + e.toString());
				return FAIL;
			}
			
			
		}else if(paramLen == 3){//执行指定时间    参数格式：kettle:path,startTime,endTime

			try {
				tsMap.put("startdate", params[1]);
				tsMap.put("enddate", params[2]);
				
				XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行路径："+ pathvalue +"-->>>>>>>>>>>>>>>>");
				XxlJobLogger.log("(定时任务执行时间为： " + nowTime );
				XxlJobLogger.log(" 与传输数据的时间校验的时间： " + params[1] + " ~ " + params[2]  );
				XxlJobLogger.log(" 传输数据的时间区间： "+ params[1] + " ~ " + params[2] + ")");
				
				KettleUtil.run(tsMap,pathvalue);
				
				XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行结束-->>>>>>>>>>>>>>>>");
				return SUCCESS;
			} catch (Exception e) {
				XxlJobLogger.log(">>>>>>>>>>>>>>>>执行失败<<<<<<<<<<<<<<<" + e.toString());
				return FAIL;
			}
			
		}else if(paramLen == 4){//指定时间偏移量   参数格式：kettle:path,time,long,long
			
			try {
				//取传入的时间范围中的数据 按天取
				if("dateRange".equals(params[1])) {
					List<String> timeList = KettleUtil.getTimeList(params[2], params[3]);
					for (String string : timeList) {
						String[] ts = string.split(",");
						tsMap.put("startdate", ts[0]);
						tsMap.put("enddate", ts[1]);
						
						XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行路径："+ pathvalue +"-->>>>>>>>>>>>>>>>");
						XxlJobLogger.log("(定时任务执行时间为： " + nowTime + ")");
						XxlJobLogger.log(" 与传输数据的时间校验的时间： " + ts[0] + " ~ " + ts[1]  );
						XxlJobLogger.log(" 传输数据的时间区间： "+ ts[0] + " ~ " + ts[1] + ")");
						
						KettleUtil.run(tsMap,pathvalue);
					}
					XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行结束-->>>>>>>>>>>>>>>>");
				}else {
					String[] ts = KettleUtil.getTime(params[1], Long.parseLong(params[2]), Long.parseLong(params[3])).split(",");
					
					tsMap.put("startdate", ts[0]);
					tsMap.put("enddate", ts[1]);
					
					XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行路径："+ pathvalue +"-->>>>>>>>>>>>>>>>");
					XxlJobLogger.log("(定时任务执行时间为： " + nowTime + ")");
					XxlJobLogger.log(" 与传输数据的时间校验的时间： " + ts[0] + " ~ " + ts[1]  );
					XxlJobLogger.log(" 传输数据的时间区间： "+ ts[0] + " ~ " + ts[1] + ")");
					
					KettleUtil.run(tsMap,pathvalue);
					
					XxlJobLogger.log("<<<<<<<<<<<<<<<<--执行结束-->>>>>>>>>>>>>>>>");
				}
				return SUCCESS;
			} catch (Exception e) {
				XxlJobLogger.log(">>>>>>>>>>>>>>>>执行失败<<<<<<<<<<<<<<<" + e.toString());
				return FAIL;
			}
		}else {
			
			XxlJobLogger.log("<<<<<<<<<<<<<<<<--参数传入错误-->>>>>>>>>>>>>>>>");
			return FAIL;
		}
		
	}
	
}
